const app = getApp()
import collectApi from '../../api/collectApi'
import videoAd from '../../util/videoAd'
import {getFishNames} from '../../util/fish'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeList : [],
    isMore:true,
    type: '',
    keyword: '',
    latitude: 0,
    longitude: 0,
  },
  pageData: {
    pageNum : 0,
    pageSize : 8
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
   
    wx.showLoading({
      title: '加载中',
    })
    await app.getOpenid()
    wx.hideLoading()

    this.getMyCollect()
   
  },

  onShow: function () { 
  },

  search(e){

    this.data.keyword = e.detail.value

    this.pageData.pageNum = 0
    
    this.setData({
      activeList: [],
      isMore: true
    })
      
    this.getMyCollect()
  },

  getMyCollect(){
    console.log('getMyCollect...')
    if(!this.data.isMore){
      return
    }
    this.pageData.pageNum ++

    var params = {
      pageNum : this.pageData.pageNum,
      pageSize : this.pageData.pageSize,
      openid: wx.getStorageSync('openid')
    }
    if(this.data.keyword){
      params.content = this.data.keyword
    }

    wx.showLoading({
      mask: true
    })
    collectApi.getCollectActiveList(params).then(res => {
      wx.hideLoading()
      console.log(res)

      if(res.length < this.pageData.pageSize){
        this.setData({
          isMore:false
        })
      }
     
      for(var item of res){
        if (typeof item.imgList == 'string'){
          item.imgList = item.imgList.split(',')
          
        }
        item.fishNames = getFishNames(item.category)
      }
      var list  = this.data.activeList.concat(res)
      this.setData({
        activeList: list
      })
    })
  },

  toDetail(e){
    var active = e.currentTarget.dataset.active
   
    wx.navigateTo({
      url: '/pages/activeDetail/activeDetail?activeId='+active.activeId,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // wx.stopPullDownRefresh( )
    // this.pageData.pageNum = 0
    // this.setData({
    //   isMore:true
    // })
    // switch(this.pageData.type){
    //   case 'myActive':
    //   this.getMyCollect()
    //   break
    //   case 'myJoinActive':
    //     this.getMyJoinActive()
    //     break
    // }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMyCollect()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '钓鱼圈',
      // imageUrl: 'https://public-server-center-prod-1258963190.cos.ap-guangzhou.myqcloud.com/studentCard/20210521153238.jpg'
    }
  }
})